package com.part2.action.common.upload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;

public class UploadProgressListener implements ProgressListener {
	
	private long megaBytes = -1;
	HttpServletRequest request = null;
	UploadInfo progressInfo = null;
	
	public UploadProgressListener(HttpServletRequest request) {
		System.out.println("<<<<<<<<<<here>>>>>>>>>>>>>");
		this.request = request;
		progressInfo = (UploadInfo)request.getSession().getAttribute("uploadInfo");
		if (progressInfo == null) {
			progressInfo = new UploadInfo();
			request.getSession().setAttribute("uploadInfo", progressInfo);
		}
	}
	
	public void update(long pBytesRead, long pContentLength, int pItems) {
		
		long mBytes = pBytesRead / 100000;
		
		if (megaBytes == mBytes) {
			return;
		}
		megaBytes = mBytes;

		if (pContentLength == -1) {
			progressInfo.setStatus("done");
		} else {
			progressInfo.setFileIndex(pItems);
			progressInfo.setTotalSize(pContentLength);
			progressInfo.setBytesRead(pBytesRead);
			progressInfo.setStatus("progress");
		}

	}

}
